<?php
/**
 * 导航分类Service
 * Date: 2025/09/10
 */

namespace App\Services\Admin;

use App\Models\CategoryModel;
use App\Services\BaseService;

class CategoryService extends BaseService
{
    /**
     * 导航分类列表
     * @date 2025/09/11
     * @param array $params 请求参数
     * @return \Illuminate\Http\JsonResponse
     */
    public function list($params)
    {
        $categoryList = CategoryModel::query()
            ->select(['id', 'pid', 'name', 'sort', 'status', 'created_at'])
            ->when(isset($params['name']) && $params['name'] != '', function ($query) use ($params) {
                $query->where('name', 'like', '%' . $params['name'] . '%');
            })
            ->when(isset($params['status']) && !empty($params['status']), function ($query) use ($params) {
                $query->where('status', $params['status']);
            })
            ->orderBy('sort', 'asc')
            ->orderBy('id', 'asc')
            ->get()
            ->toArray();
        $categoryList = buildTree($categoryList, 'id');
        return $this->success($categoryList);
    }

    /**
     * 导航分类创建
     * @date 2025/09/11
     * @param array $params 请求参数
     * @return \Illuminate\Http\JsonResponse
     */
    public function create($params)
    {
        // 父级分类判断
        if ($params['pid'] != 0) {
            $parentCategory = CategoryModel::query()->where('id', $params['pid'])->first();
            if (empty($parentCategory)) {
                return $this->failed('父级分类不存在');
            }

            if ($parentCategory->pid != 0) {
                return $this->failed('分类级别最多是二级分类');
            }
        }

        // 分类创建
        CategoryModel::query()->create([
            'pid' => $params['pid'],
            'name' => $params['name'],
            'icon' => $params['icon'] ?? '',
            'sort' => $params['sort'],
            'status' => $params['status']
        ]);
        return $this->success();
    }

    /**
     * 导航分类详情
     * @date 2025/09/11
     * @param int $id 分类主键ID
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail($id)
    {
        // 检测导航分类信息
        if (!CategoryModel::query()->where('id', $id)->exists()) {
            return $this->failed('分类不存在');
        }

        // 导航分类详情
        $adminInfo = CategoryModel::query()
            ->select(['id', 'name', 'icon', 'sort', 'status'])
            ->where('id', $id)
            ->first();
        return $this->success($adminInfo);
    }

    /**
     * 导航分类更新
     * @date 2025/09/11
     * @param array $params 请求参数
     * @return \Illuminate\Http\JsonResponse
     */
    public function update($params)
    {
        // 检测导航分类信息
        if (!CategoryModel::query()->where('id', $params['id'])->exists()) {
            return $this->failed('分类不存在');
        }

        // 导航分类更新
        CategoryModel::query()->where('id', $params['id'])->update([
            'name' => $params['name'],
            'icon' => $params['icon'] ?? '',
            'sort' => $params['sort'],
            'status' => $params['status']
        ]);
        return $this->success();
    }

    /**
     * 导航分类删除
     * @date 2025/09/11
     * @param int $id 分类主键ID
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete($id)
    {
        // 检测导航分类信息
        if (!CategoryModel::query()->where('id', $id)->exists()) {
            return $this->failed('分类不存在');
        }

        // 导航分类删除
        CategoryModel::query()->where('id', $id)->delete();
        CategoryModel::query()->where('pid', $id)->delete();
        return $this->success();
    }
}
